package com.obor.quartz.config;

import java.io.IOException;
import java.util.Properties;

import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.springframework.beans.factory.config.PropertiesFactoryBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;
import org.zn.mvcbase.rest.RestEntityFactroy;

import com.obor.quartz.job.JobManager;

/**
* 
* @Company OBOR
* @Author 周楠
* @Description
* 2019年3月1日
*
*/
@Configuration
public class QuartzConfigration {

	
	    @Bean
	    public SchedulerFactoryBean schedulerFactoryBean() {
	        SchedulerFactoryBean schedulerFactoryBean = new SchedulerFactoryBean();
	        try {
	            schedulerFactoryBean.setOverwriteExistingJobs(true);
	            schedulerFactoryBean.setQuartzProperties(quartzProperties());
	         //   schedulerFactoryBean.setJobFactory();
	        } catch (Exception e) {
	            e.printStackTrace();
	        }
	        
	        return schedulerFactoryBean;
	    }

	    // 指定quartz.properties，可在配置文件中配置相关属性
	    public Properties quartzProperties() throws IOException {
	        PropertiesFactoryBean propertiesFactoryBean = new PropertiesFactoryBean();
	        propertiesFactoryBean.setLocation(new ClassPathResource("/quartz.properties"));
	        propertiesFactoryBean.afterPropertiesSet();
	        return propertiesFactoryBean.getObject();
	    }

	    
	    @Bean
	    public  JobManager jobManager(Scheduler scheduler,RestEntityFactroy ref) {
	    	
	    	JobManager job=	new JobManager();
	    	
	    	job.setScheduler(scheduler);
	    	
	    	try {
				scheduler.getContext().put("REF", ref);
			} catch (SchedulerException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	    	
			return job;
	    	
	    }
	    
}
